home *** CD-ROM | disk | FTP | other *** search
/ MacTech 1 to 12 / MacTech-vol-1-12.toast / Source / MacTech® Magazine / Volume 07 - 1991 / Goodies Disk / Glypha.src / Code ƒ / B-Dialogs.p < prev    next >
Encoding:
Text File  |  1991-02-06  |  16.2 KB  |  489 lines  |  [TEXT/PJMM]

  1. {This code was written by John Calhoun of Soft Dorothy Software.  Code is ©1990 john calhoun}
  2. {Feel free to read over the code, learn from it, etc.}
  3. {If the code helps you or if you borrow portions of it, please send $15 to:}
  4. {john calhoun}
  5. {1201 Oread #4}
  6. {Lawrence, KS 66044}
  7. {So long as this header remains, feel free to distribute this code.}
  8.  
  9. unit Dialogs;
  10.  
  11. interface
  12.  
  13.     var
  14.         rightOffset, downOffset: Integer;
  15.  
  16.     procedure D_ConfigureGameWndo (var mortals, level, toDelay: Integer; var soundOn: Boolean; cantSing: Boolean);
  17.     procedure D_ControlsWndo (var keyboardControl: Boolean);
  18.     procedure WhosHiScore (var theName: Str255);
  19.  
  20. implementation
  21.  
  22.     const                            {These are the item numbers for controls in the Dialog    }
  23.         I_Okay = 1;                    {They are found in the .RSRC fork of Glypha                }
  24.         I_Cancel = 2;
  25.         I_Sound_OFF = 3;
  26.         I_Sound_ON = 4;
  27.         I_Fast = 5;
  28.         I_Fastest = 6;
  29.         I_Slow = 14;
  30.         I_Slowest = 15;
  31.  
  32.         I_Static_Text = 2;
  33.         I_Static_Text3 = 3;
  34.         I_Static_Text7 = 7;
  35.         I_Static_Text8 = 8;
  36.         I_Static_Text10 = 9;
  37.         I_Edit_Text = 4;
  38.         I_Edit_Text6 = 5;
  39.         I_Edit_Text10 = 10;
  40.         I_Edit_Text13 = 11;
  41.         I_Rectanglex12 = 12;
  42.         I_Rectanglex13 = 13;
  43.         I_Drawn_line = 14;
  44.         I_Drawn_line18 = 15;
  45.         I_Keyboard = 3;
  46.         I_Mouse = 4;
  47.         I_Static_Text5 = 5;
  48.         I_Iconx6 = 6;
  49.         I_Iconx7 = 7;
  50.         I_Iconx8 = 8;
  51.         I_Iconx9 = 9;
  52.         I_Iconx10 = 10;
  53.         I_Iconx11 = 11;
  54.         I_Iconx12 = 12;
  55.         I_Iconx13 = 13;
  56.         I_Rectanglex14 = 14;
  57.         I_Rectanglex15 = 15;
  58.         I_Rectanglex16 = 16;
  59.     var
  60.         GetSelection: DialogPtr;             {Name of dialog}
  61.         tempRect: Rect;                      {Temporary tectangle}
  62.         DType: Integer;                      {Type of dialog item}
  63.         Index: Integer;                      {For looping}
  64.         DItem: Handle;                       {Handle to the dialog item}
  65.         CItem, CTempItem: controlhandle;     {Control handle}
  66.         sTemp: Str255;                       {Get text entered, temp holding}
  67.         itemHit: Integer;                    {Get selection}
  68.         temp: Integer;                       {Get selection, temp holding}
  69.         ThisEditText: TEHandle;              {Handle to get the Dialogs TE record}
  70.         TheDialogPtr: DialogPeek;            {Pointer to Dialogs definition record, contains the TE record}
  71.         ExitDialog, keepIt: boolean;
  72.  
  73. {==========================================================}
  74.  
  75.     procedure D_ConfigureGameWndo;
  76.         var
  77.             soundCopy, keepIt: Boolean;
  78.             delayCopy, levelCopy, mortalsCopy, rightOff, downOff, Index: Integer;
  79.             theValue: Integer;
  80.             nTemp: LongInt;
  81.         procedure Refresh_Dialog;            {Refresh the dialogs non-controls    }
  82.             var
  83.                 rTempRect: Rect;
  84.         begin
  85.             rTempRect := tempRect;
  86.             PenSize(1, 1);
  87.             MoveTo(8, 80);
  88.             LineTo(340, 80);
  89.             MoveTo(171, 40);
  90.             LineTo(171, 134);
  91.             SetRect(tempRect, 6, 40, 341, 135);
  92.             FrameRect(tempRect);
  93.             MoveTo(7, 29);
  94.             LineTo(340, 29);
  95.             LineTo(340, 6);
  96.             SetRect(tempRect, 6, 5, 340, 29);
  97.             FrameRect(tempRect);
  98.             SetRect(tempRect, 222, 146, 299, 173);
  99.             PenSize(3, 3);
  100.             FrameRoundRect(tempRect, 15, 15);
  101.             PenSize(1, 1);
  102.             tempRect := rTempRect;
  103.         end;
  104.  
  105.     begin
  106.         levelCopy := level;
  107.         mortalsCopy := mortals;
  108.         soundCopy := soundOn;
  109.         delayCopy := toDelay;
  110.         GetSelection := GetNewDialog(3, nil, Pointer(-1));    {Bring in the dialog resource        }
  111.         with GetSelection^.portBits do
  112.             begin
  113.                 rightOff := rightOffset - bounds.left;
  114.                 downOff := downOffset - bounds.top;
  115.             end;
  116.         MoveWindow(GetSelection, rightOff, downOff, FALSE);
  117.         ShowWindow(GetSelection);
  118.         SelectWindow(GetSelection);                        {Lets see it                            }
  119.         SetPort(GetSelection);                                {Perpare to add conditional text    }
  120.         TheDialogPtr := DialogPeek(GetSelection);{Get to the inner record            }
  121.         ThisEditText := TheDialogPtr^.textH;    {Get to the TE record            }
  122.         HLock(Handle(ThisEditText));                    {Lock it for safety                }
  123.         ThisEditText^^.txSize := 12;                    {TE Point size                        }
  124.         TextSize(12);                                                    {Window Point size                }
  125.         ThisEditText^^.txFont := systemFont;    {TE Font ID                                }
  126.         TextFont(systemFont);                                    {Window Font ID                        }
  127.         ThisEditText^^.txFont := 0;                        {TE Font ID                                }
  128.         ThisEditText^^.fontAscent := 12;            {Font ascent                            }
  129.         ThisEditText^^.lineHeight := 12 + 3 + 1;{Font ascent + descent + leading    }
  130.         HUnLock(Handle(ThisEditText));                {UnLock handle when done    }
  131.         if (soundOn) then
  132.             begin
  133.                 GetDItem(GetSelection, I_Sound_ON, DType, DItem, tempRect);
  134.                 CItem := Pointer(DItem);
  135.                 SetCtlValue(CItem, 1);
  136.             end
  137.         else
  138.             begin
  139.                 GetDItem(GetSelection, I_Sound_OFF, DType, DItem, tempRect);
  140.                 CItem := Pointer(DItem);
  141.                 SetCtlValue(CItem, 1);
  142.             end;
  143.         if (cantSing) then                    {cantSing is TRUE if run under older than System 6.0    }
  144.             begin
  145.                 GetDItem(GetSelection, I_Sound_ON, DType, DItem, tempRect);
  146.                 CItem := Pointer(DItem);
  147.                 HiliteControl(CItem, 255);
  148.                 GetDItem(GetSelection, I_Sound_OFF, DType, DItem, tempRect);
  149.                 CItem := Pointer(DItem);
  150.                 HiliteControl(CItem, 255);
  151.             end;
  152.  
  153.         case toDelay of
  154.             0: 
  155.                 GetDItem(GetSelection, I_Fastest, DType, DItem, tempRect);
  156.             1: 
  157.                 GetDItem(GetSelection, I_Fast, DType, DItem, tempRect);
  158.             3: 
  159.                 GetDItem(GetSelection, I_Slow, DType, DItem, tempRect);
  160.             4: 
  161.                 GetDItem(GetSelection, I_Slowest, DType, DItem, tempRect);
  162.             otherwise
  163.                 begin
  164.                     GetDItem(GetSelection, I_Fastest, DType, DItem, tempRect);
  165.                 end;
  166.         end;
  167.         CItem := Pointer(DItem);
  168.         SetCtlValue(CItem, 1);            {Select to slower}
  169.  
  170.         GetDItem(GetSelection, I_Edit_Text10, DType, DItem, tempRect);
  171.         NumToString(mortalsCopy, sTemp);
  172.         SetIText(DItem, sTemp);
  173.         GetDItem(GetSelection, I_Edit_Text13, DType, DItem, tempRect);
  174.         NumToString(levelCopy, sTemp);
  175.         SetIText(DItem, sTemp);
  176.         SelIText(GetSelection, I_Edit_Text13, 0, 2);
  177.         Refresh_Dialog;
  178.         ExitDialog := FALSE;                    {Do not exit dialog handle loop yet    }
  179.         repeat                                                {Start of dialog handle loop                }
  180.             ModalDialog(nil, itemHit);    {Wait until an item is hit                    }
  181.             GetDItem(GetSelection, itemHit, DType, DItem, tempRect);    {Get item information    }
  182.             CItem := Pointer(DItem);        {Get the control handle                            }
  183.             if (ItemHit = I_Okay) then    {Handle the Button being pressed                    }
  184.                 begin
  185.                     GetDItem(GetSelection, I_Edit_Text10, DType, DItem, tempRect); {Get the item handle}
  186.                     GetIText(DItem, sTemp);
  187.                     StringToNum(sTemp, nTemp);
  188.                     mortalsCopy := LoWord(nTemp);
  189.  
  190.                     GetDItem(GetSelection, I_Edit_Text13, DType, DItem, tempRect); {Get the item handle}
  191.                     GetIText(DItem, sTemp);            {Get the text entered                                }
  192.                     StringToNum(sTemp, nTemp);
  193.                     levelCopy := LoWord(nTemp);
  194.  
  195.                     if ((levelCopy < 1) or (levelCopy > 99)) then
  196.                         begin
  197.                             GetDItem(GetSelection, I_Edit_Text13, DType, DItem, tempRect);
  198.                             NumToString(levelCopy, sTemp);
  199.                             SetIText(DItem, sTemp);
  200.                             levelCopy := level;
  201.                         end;
  202.                     if ((mortalsCopy < 1) or (mortalsCopy > 99)) then
  203.                         begin
  204.                             GetDItem(GetSelection, I_Edit_Text10, DType, DItem, tempRect);
  205.                             NumToString(mortalsCopy, sTemp);
  206.                             SetIText(DItem, sTemp);
  207.                             mortalsCopy := mortals;
  208.                         end;
  209.                     if ((mortalsCopy > 0) and (mortalsCopy < 100)) and ((levelCopy > 0) and (levelCopy < 100)) then
  210.                         begin
  211.                             level := levelCopy;
  212.                             mortals := mortalsCopy;
  213.                             keepIt := TRUE;
  214.                             ExitDialog := TRUE;
  215.                         end;
  216.                     Refresh_Dialog;
  217.                 end;
  218.             if (ItemHit = I_Cancel) then
  219.                 begin
  220.                     keepIt := FALSE;
  221.                     ExitDialog := TRUE;
  222.                     Refresh_Dialog;
  223.                 end;
  224.             if (ItemHit >= I_Sound_OFF) and (ItemHit <= I_Sound_ON) then {Handle the Radio selection}
  225.                 begin
  226.                     for Index := I_Sound_OFF to I_Sound_ON do {Clear all other radios}
  227.                         begin
  228.                             GetDItem(GetSelection, Index, DType, DItem, tempRect); {Get the Radio handle}
  229.                             CTempItem := Pointer(DItem);    {Convert to a control handle}
  230.                             SetCtlValue(CTempItem, 0);      {Turn the radio selection OFF}
  231.                         end;                              {End of clear the radio selections loop}
  232.                     SetCtlValue(CItem, 1);            {Turn the one radio selection ON}
  233.                     GetDItem(GetSelection, I_Sound_OFF, DType, DItem, tempRect);
  234.                     theValue := GetCtlValue(Pointer(DItem));
  235.                     if (theValue = 1) then
  236.                         soundCopy := FALSE
  237.                     else
  238.                         soundCopy := TRUE;
  239.                 end;
  240.             if ((ItemHit = I_Slowest) or (ItemHit = I_Fastest) or (ItemHit = I_Fast) or (ItemHit = I_Slow)) then {Handle the Radio selection}
  241.                 begin
  242.                     GetDItem(GetSelection, I_Slowest, DType, DItem, tempRect);    {Get the Radio handle}
  243.                     CTempItem := Pointer(DItem);                                        {Convert to a control handle            }
  244.                     SetCtlValue(CTempItem, 0);                                        {Turn the radio selection OFF                }
  245.                     GetDItem(GetSelection, I_Fastest, DType, DItem, tempRect);    {Get the Radio handle}
  246.                     CTempItem := Pointer(DItem);                                        {Convert to a control handle            }
  247.                     SetCtlValue(CTempItem, 0);                                        {Turn the radio selection OFF                }
  248.                     GetDItem(GetSelection, I_Fast, DType, DItem, tempRect);        {Get the Radio handle}
  249.                     CTempItem := Pointer(DItem);                                        {Convert to a control handle            }
  250.                     SetCtlValue(CTempItem, 0);                                        {Turn the radio selection OFF                }
  251.                     GetDItem(GetSelection, I_Slow, DType, DItem, tempRect);        {Get the Radio handle}
  252.                     CTempItem := Pointer(DItem);                                        {Convert to a control handle            }
  253.                     SetCtlValue(CTempItem, 0);                                        {Turn the radio selection OFF                }
  254.  
  255.                                                                     {End of clear the radio selections loop    }
  256.                     SetCtlValue(CItem, 1);                                                    {Turn the one radio selection ON    }
  257.  
  258.                     GetDItem(GetSelection, I_Fastest, DType, DItem, tempRect);
  259.                     theValue := GetCtlValue(Pointer(DItem));
  260.                     if (theValue = 1) then
  261.                         delayCopy := 0;
  262.  
  263.                     GetDItem(GetSelection, I_Fast, DType, DItem, tempRect);
  264.                     theValue := GetCtlValue(Pointer(DItem));
  265.                     if (theValue = 1) then
  266.                         delayCopy := 1;
  267.  
  268.                     GetDItem(GetSelection, I_Slow, DType, DItem, tempRect);
  269.                     theValue := GetCtlValue(Pointer(DItem));
  270.                     if (theValue = 1) then
  271.                         delayCopy := 3;
  272.  
  273.                     GetDItem(GetSelection, I_Slowest, DType, DItem, tempRect);
  274.                     theValue := GetCtlValue(Pointer(DItem));
  275.                     if (theValue = 1) then
  276.                         delayCopy := 4;
  277.  
  278.                 end;
  279.         until ExitDialog;
  280.         if (keepIt) then
  281.             begin
  282.                 soundOn := soundCopy;
  283.                 toDelay := delayCopy;
  284.             end;
  285.         DisposDialog(GetSelection);
  286.     end;
  287.  
  288. {===========================================================}
  289.  
  290.     function MyFilter (theDialog: DialogPtr; var theEvent: EventRecord; var itemHit: integer): boolean;
  291.         var
  292.             MyPt: Point;
  293.     begin
  294.         MyFilter := FALSE;
  295.         if (theEvent.what = MouseDown) then
  296.             begin
  297.                 MyPt := theEvent.where;
  298.                 with theDialog^.portBits.bounds do
  299.                     begin
  300.                         myPt.h := myPt.h + left;
  301.                         myPt.v := myPt.v + top;
  302.                     end;
  303.             end;
  304.     end;
  305.  
  306. {===========================================================}
  307.  
  308.     procedure D_ControlsWndo;
  309.  
  310.         var
  311.             Index, rightOff, downOff: Integer;
  312.  
  313.         procedure Refresh_Dialog;
  314.             var
  315.                 rTempRect: Rect;
  316.         begin
  317.             rTempRect := tempRect;
  318.             PenSize(1, 1);
  319.             MoveTo(127, 114);
  320.             LineTo(265, 114);
  321.             LineTo(265, 36);
  322.             SetRect(TempRect, 126, 35, 265, 114);
  323.             FrameRect(TempRect);
  324.             MoveTo(127, 164);
  325.             LineTo(265, 164);
  326.             LineTo(265, 121);
  327.             SetRect(TempRect, 126, 120, 265, 164);
  328.             FrameRect(TempRect);
  329.             MoveTo(7, 32);
  330.             LineTo(267, 32);
  331.             LineTo(267, 6);
  332.             SetRect(TempRect, 6, 5, 267, 32);
  333.             FrameRect(TempRect);
  334.             SetRect(tempRect, 172, 176, 257, 203);
  335.             PenSize(3, 3);
  336.             FrameRoundRect(tempRect, 15, 15);
  337.             PenSize(1, 1);
  338.             tempRect := rTempRect;
  339.         end;
  340.  
  341.     begin
  342.         GetSelection := GetNewDialog(4, nil, Pointer(-1));
  343.         with GetSelection^.portBits do
  344.             begin
  345.                 rightOff := rightOffset - bounds.left;
  346.                 downOff := downOffset - bounds.top;
  347.             end;
  348.         MoveWindow(GetSelection, rightOff, downOff, FALSE);
  349.         ShowWindow(GetSelection);
  350.         SelectWindow(GetSelection);
  351.         SetPort(GetSelection);
  352.         TheDialogPtr := DialogPeek(GetSelection);
  353.         ThisEditText := TheDialogPtr^.textH;
  354.         HLock(Handle(ThisEditText));
  355.         ThisEditText^^.txSize := 12;
  356.         TextSize(12);
  357.         ThisEditText^^.txFont := systemFont;
  358.         TextFont(systemFont);
  359.         ThisEditText^^.txFont := 0;
  360.         ThisEditText^^.fontAscent := 12;
  361.         ThisEditText^^.lineHeight := 12 + 3 + 1;
  362.         HUnLock(Handle(ThisEditText));
  363.         if (keyboardControl) then
  364.             GetDItem(GetSelection, I_Keyboard, DType, DItem, tempRect)
  365.         else
  366.             GetDItem(GetSelection, I_Mouse, DType, DItem, tempRect);
  367.         CItem := Pointer(DItem);
  368.         SetCtlValue(CItem, 1);
  369.         Refresh_Dialog;
  370.         ExitDialog := FALSE;
  371.         repeat
  372.             ModalDialog(nil, itemHit);
  373.             GetDItem(GetSelection, itemHit, DType, DItem, tempRect);
  374.             CItem := Pointer(DItem);
  375.             if (ItemHit = I_Okay) then
  376.                 begin
  377.                     keepIt := TRUE;
  378.                     ExitDialog := TRUE;
  379.                     Refresh_Dialog;
  380.                 end;
  381.             if (ItemHit = I_Cancel) then
  382.                 begin
  383.                     keepIt := FALSE;
  384.                     ExitDialog := TRUE;
  385.                     Refresh_Dialog;
  386.                 end;
  387.             if (ItemHit >= I_Keyboard) and (ItemHit <= I_Mouse) then
  388.                 begin
  389.                     for Index := I_Keyboard to I_Mouse do
  390.                         begin
  391.                             GetDItem(GetSelection, Index, DType, DItem, tempRect);
  392.                             CTempItem := Pointer(DItem);
  393.                             SetCtlValue(CTempItem, 0);
  394.                         end;
  395.                     SetCtlValue(CItem, 1);
  396.                 end;
  397.         until ExitDialog;
  398.         Index := I_Keyboard;
  399.         repeat
  400.             GetDItem(GetSelection, Index, DType, DItem, tempRect);
  401.             CItem := Pointer(DItem);
  402.             temp := GetCtlValue(CItem);
  403.             Index := Index + 1;
  404.         until (temp <> 0) or (Index > I_Mouse);
  405.         temp := Index - I_Keyboard + 1;
  406.         if (keepIt) then
  407.             begin
  408.                 if (temp = 3) then
  409.                     keyboardControl := FALSE
  410.                 else
  411.                     keyboardControl := TRUE;
  412.             end;
  413.         DisposDialog(GetSelection);
  414.     end;
  415.  
  416. {===========================================}
  417.  
  418.     procedure WhosHiScore;
  419.         var
  420.             excessSpace, rightOff, downOff: Integer;
  421.             dotFiller, space: Str255;
  422.  
  423.         procedure Refresh_Dialog;                {This draws the rounded-rectangular        }
  424.             var                                            {border around the Okay button so that        }
  425.                 rTempRect: Rect;                            {the user knows it is the default button.        }
  426.         begin
  427.             PenNormal;
  428.             PenSize(3, 3);
  429.             SetRect(rTempRect, 167, 36, 251, 65);
  430.             FrameRoundRect(rTempRect, 13, 13);
  431.         end;
  432.  
  433.     begin                                                            {Start of dialog handler}
  434.         GetSelection := GetNewDialog(999, nil, Pointer(-1));{Bring in the dialog resource}
  435.         with GetSelection^.portBits do
  436.             begin
  437.                 rightOff := rightOffset - bounds.left;
  438.                 downOff := downOffset - bounds.top;
  439.             end;
  440.         MoveWindow(GetSelection, rightOff, downOff, FALSE);
  441.         ShowWindow(GetSelection);
  442.         SelectWindow(GetSelection);                                {Lets see it}
  443.         SetPort(GetSelection);                                        {Perpare to add conditional text}
  444.         TheDialogPtr := DialogPeek(GetSelection);    {Get to the inner record}
  445.         ThisEditText := TheDialogPtr^.textH;            {Get to the TE record}
  446.         HLock(Handle(ThisEditText));                            {Lock it for safety}
  447.         ThisEditText^^.txSize := 12;                            {TE Point size}
  448.         TextSize(12);                                                            {Window Point size}
  449.         ThisEditText^^.txFont := systemFont;          {TE Font ID}
  450.         TextFont(systemFont);                                            {Window Font ID}
  451.         ThisEditText^^.txFont := 0;                                {TE Font ID}
  452.         ThisEditText^^.fontAscent := 12;                    {Font ascent}
  453.         ThisEditText^^.lineHeight := 12 + 3 + 1;    {Font ascent + descent + leading}
  454.         HUnLock(Handle(ThisEditText));                        {UnLock the handle when done}
  455.         GetDItem(GetSelection, I_Edit_Text, DType, DItem, tempRect);
  456.         SetIText(DItem, theName);                                    {Set the default text}
  457.         SelIText(GetSelection, I_Edit_Text, 0, 15);{Select the text}
  458.         ExitDialog := FALSE;                                            {Don't exit dialog loop yet}
  459.         Refresh_Dialog;
  460.         repeat                                                                        {Start of dialog loop}
  461.             ModalDialog(nil, itemHit);                            {Wait until item is hit}
  462.             GetDItem(GetSelection, itemHit, DType, DItem, tempRect); {Get item information}
  463.             CItem := Pointer(DItem);                                {Get the control handle}
  464.             if (ItemHit = I_Okay) then                            {Handle the Button pressed}
  465.                 begin
  466.                     GetDItem(GetSelection, I_Edit_Text, DType, DItem, tempRect); {Get the item handle}
  467.                     GetIText(DItem, sTemp);                            {Get the text entered}
  468.                     if (LENGTH(sTemp) > 15) then                {Make sure it's less than 15 characters}
  469.                         theName := COPY(sTemp, 1, 15)            {Just clip the first 15 if it is too long}
  470.                     else
  471.                         begin
  472.                             if (LENGTH(sTemp) < 15) then
  473.                                 begin
  474.                                     space := '               ';
  475.                                     excessSpace := 15 - LENGTH(sTemp);
  476.                                     dotFiller := COPY(space, 1, excessSpace);
  477.                                     sTemp := CONCAT(sTemp, dotfiller);
  478.                                 end;
  479.                             theName := sTemp;                            {Otherwise, take it as is}
  480.                         end;
  481.                     ExitDialog := TRUE;                                {Exit the dialog when this selection is made}
  482.                 end;                                                {End for this item selected}
  483.         until ExitDialog;                                        {Handle dialog items until exit selected}
  484.         DisposDialog(GetSelection);                        {Flush the dialog out of memory}
  485.     end;                                                        {End of procedure}
  486.  
  487. {=============================================================}
  488.  
  489. end.